const tbody = document.querySelector('#tbody-shop');
const btnLogin = document.querySelector('#btn-login');
const welcome = document.querySelector('#welcome');
const i = welcome.querySelector('i');
const logOut=document.querySelector('#log-out');
getData();
function getData() {
    utils.fetch('api/shop/select.php').then((resp) => {
        if (resp.code === 200) {
            const { list } = resp.body;
            tbody.innerHTML = list.reduce((html, person, index) => {
                return html + `
                    <tr data-id="${person.id}">
                        <td>${index + 1}</td>
                        <td><span>${person.name}</span><input class="editName" type="text"> </td>
                        <td><span>${person.price}</span><input class="editPrice" type="text"></td>
                        <td><span>${person.num}</span><input type="text" class="editNum"></td>
                        <td>
                            <button class="btn btn-info btn-xs btn-edit">编辑</button>
                            <button class="btn btn-danger btn-xs btn-del">删除</button>
                            <button class="btn btn-success btn-xs btn-ok">确定</button>
                            <button class="btn btn-warning btn-xs btn-cancel">取消</button>
                        </td>
                    </tr>
                `
            }, '')
        } else {
            alert('网络连接失败');
        }
    })

}
var cookName = utils.getCookie('name');
if (cookName) {
    welcome.classList.remove('btnHide');
    btnLogin.classList.add('btnHide');
    i.innerHTML = cookName;
    utils.on(logOut, 'click', function () {
        if (confirm('确定退出吗')) {
            utils.setCookie('name', '', { expires: -1, path: '/' })
            welcome.classList.remove('btnHide');
            btnLogin.classList.add('btnHide');
            window.open('index.html', '_self');
        }
    })
}
